package com.level.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.level.system.base.ResponseBean;
import com.level.system.dao.EmrMap;
import com.level.system.entity.emr.*;
import com.level.system.entity.enums.emr.EMR_FYEnums;
import com.level.system.entity.enums.emr.HISUploadDataEnums;
import com.level.system.entity.enums.emr.HealthCareEnums;
import com.level.system.service.HISUploadService;
import com.level.system.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.net.URLDecoder;
import java.sql.Clob;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
public class HISUploadServiceImpl extends ServiceImpl<EmrMap, PATIENT_INFORMATION> implements HISUploadService{

    @Resource
    private EmrMap emrMap;

    private final Logger log = LoggerFactory.getLogger(HISUploadServiceImpl.class);

    /**
     * 同步病人基本信息
     * upPatientInfo（patientInfo） 存在更新不存在插入
     * upPuPatVisiting(patientInfo) 存在更新不存在插入
     * upEmrBasy(patientInfo)       存在更新不存在插入
     * @param bean
     */
    @Override
    @Transactional
    public ResponseBean upPatientInfo(PATIENT_INFORMATION bean) {
        if(bean.getSysdate()==null){
            bean.setSysdate(new Date());
        }
        PATIENT_INFORMATION temp_basy = emrMap.queryEmrBasy(bean);
        log.info("1病人id:"+bean.getPatientId()+",病人住院号："+bean.getMcNo()+",机构id："+bean.getJgid()+",病案首页是否有记录："+temp_basy);
        try{
            if(temp_basy!=null){
                emrMap.updateEmrBasy(bean,temp_basy);
            }else{
                emrMap.insertEmrBasy(bean);
            }
        }catch(Exception e){
            e.printStackTrace();
            //保存日志，返回数据
            return ResponseBean.fail(HISUploadDataEnums.FAIL_DB_EMRBASY.getCode(),HISUploadDataEnums.FAIL_DB_EMRBASY.getDescription(),e.getMessage());
        }
        try{
            PATIENT_INFORMATION temp_pat = emrMap.queryPatVisiting(bean);
            if(temp_pat!=null){
                bean.setEmpId(temp_pat.getEmpId());
                emrMap.updatePatVisiting(bean);
                emrMap.updatePatientInfo(bean);
            }else{
                bean.setEmpId(UUID.randomUUID() + "");
                emrMap.insertPatientInfo(bean);
                emrMap.insertPatVisiting(bean);
            }
        }catch(Exception e){
            e.printStackTrace();
            //保存日志，返回数据
            return ResponseBean.fail(HISUploadDataEnums.FAIL_DB_EMRPAT.getCode(),HISUploadDataEnums.FAIL_DB_EMRPAT.getDescription(),e.getMessage());
        }
        return ResponseBean.success("同步病人基本信息成功");
    }

    @Override
    public void upDicCWBDXX(String data) {
        List<CWBDXX> list=JSON.parseArray(data,CWBDXX.class);
        if(list.size()>0){
            emrMap.deleteCWBDXX(list.get(0));
            emrMap.saveCWBDXX(list);
        }
    }

    /**
     * 同步婴儿信息
     * @param data
     * @return
     */
    @Override
    @Transactional
    public ResponseBean upInfant(String data) {
        /**
         * 转化实体
         */
        JSONObject jo = JSONObject.parseObject(data);
        InfantBean bean = JSON.parseObject(jo.get("data").toString(),InfantBean.class);
        InfantBean temp = emrMap.queryinfant(bean);
        try{
            if(temp!=null){
                emrMap.updateinfant(bean);
            }else{
                emrMap.insertinfant(bean);
            }
        }catch(Exception e){
            //保存日志，返回数据
            return ResponseBean.fail(HISUploadDataEnums.FAIL_DB_EMRBASY.getCode(),HISUploadDataEnums.FAIL_DB_EMRBASY.getDescription(),e.getMessage());
        }
        return ResponseBean.success("同步婴儿信息成功");
    }

    @Override
    public ResponseBean upDicBQXX(String data) {
        JSONObject jo = JSONObject.parseObject(data);
        JSONArray arr = JSONObject.parseArray(jo.get("list").toString());
        List<BQDM> list = new ArrayList<>();
        ResponseBean result = ResponseBean.success("同步病区信息成功");
        for(int i = 0; i < arr.size(); i++) {
            JSONObject obj = arr.getJSONObject(i);
            BQDM bean = JSON.parseObject(obj.toString(), BQDM.class);
            BQDM temp = emrMap.queryBqxx(bean);
            try{
                if(temp!=null){
                    emrMap.updateBqxx(bean);
                }else{
                    emrMap.insertBqxx(bean);
                }
            }catch(Exception e){
                //保存日志，返回数据,将不成功的信息返回
                list.add(bean);
            }
        }
        if(list.size()>0){
            result.setCode(HISUploadDataEnums.FAIL_DB_WARD.getCode());
            result.setData(list);
            result.setMsg(HISUploadDataEnums.FAIL_DB_WARD.getDescription());
        }
        return result;
    }

    @Override
    public ResponseBean upPatientSSInfo(String data) {
        JSONObject jo = JSONObject.parseObject(data);
        log.info("手术同步信息："+jo);
        JSONArray arr = JSONObject.parseArray(jo.get("list").toString());
        List<OperationList> list = new ArrayList<>();
        ResponseBean result = ResponseBean.success("同步病人手术信息成功");
        for(int i = 0; i < arr.size(); i++) {
            JSONObject obj = arr.getJSONObject(i);
            OperationList bean = JSON.parseObject(obj.toString(), OperationList.class);
            OperationList temp = emrMap.queryPatientSSInfo(bean);
            try{
                if(temp!=null){
                    emrMap.updatePatientSSInfo(bean,temp);
                }else{
                    emrMap.insertPatientSSInfo(bean);
                }
            }catch(Exception e){
                //保存日志，返回数据,将不成功的信息返回
                e.printStackTrace();
                list.add(bean);
            }
        }
        if(list.size()>0){
            result.setCode(HISUploadDataEnums.FAIL_DB_SURGERY.getCode());
            result.setData(list);
            result.setMsg(HISUploadDataEnums.FAIL_DB_SURGERY.getDescription());
        }
        return result;
    }

    @Override
    public ResponseBean upDicKSXX(String data) {
        JSONObject jo = JSONObject.parseObject(data);
        JSONArray arr = JSONObject.parseArray(jo.get("list").toString());
        List<KSDM> list = new ArrayList<>();
        ResponseBean result = ResponseBean.success("同步科室信息成功");
        for(int i = 0; i < arr.size(); i++) {
            JSONObject obj = arr.getJSONObject(i);
            KSDM bean = JSON.parseObject(obj.toString(), KSDM.class);
            KSDM temp = emrMap.queryKsdm(bean);
            try{
                if(temp!=null){
                    emrMap.updateKsdm(bean);
                }else{
                    emrMap.insertKsdm(bean);
                }
            }catch(Exception e){
                //保存日志，返回数据,将不成功的信息返回
                list.add(bean);
            }
        }
        if(list.size()>0){
            result.setCode(HISUploadDataEnums.FAIL_DB_WARD.getCode());
            result.setData(list);
            result.setMsg(HISUploadDataEnums.FAIL_DB_WARD.getDescription());
        }
        return result;
    }

    @Override
    public ResponseBean upDicYSXX(String data) {
        JSONObject jo = JSONObject.parseObject(data);
        JSONArray arr = JSONObject.parseArray(jo.get("list").toString());
        List<ZGXX> list = new ArrayList<>();
        ResponseBean result = ResponseBean.success("同步医生信息成功");
        for(int i = 0; i < arr.size(); i++) {
            JSONObject obj = arr.getJSONObject(i);
            ZGXX bean = JSON.parseObject(obj.toString(), ZGXX.class);
            log.info("==========================同步（职工）医生信息传参impl："+bean.toString());
            ZGXX temp = emrMap.querydoctroInfo(bean);
            try{
                if(temp!=null){
                    emrMap.updatedoctroInfo(bean);
                }else{
                    emrMap.insertdoctroInfo(bean);
                }
            }catch(Exception e){
                //保存日志，返回数据,将不成功的信息返回
                list.add(bean);
            }
        }
        if(list.size()>0){
            result.setCode(HISUploadDataEnums.FAIL_DB_WARD.getCode());
            result.setData(list);
            result.setMsg(HISUploadDataEnums.FAIL_DB_WARD.getDescription());
        }
        return result;
    }

    /**
     * 查询医保上传数据--门诊、住院
     * 生命体征、病案
     * @param jgid
     * @param mcno
     * @param docid
     * @param emrtype
     * @return
     */
    @Override
    public ResponseBean healthCare(String jgid,String mcno,String docid,String emrtype,String hzflag,String jgno) {
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> parem = new HashMap<>();
        Map<String,Object> basy_fy = null;
        parem.put("jgid",jgid);
        parem.put("mcno",mcno);
        parem.put("docid",docid);
        try {
            if (HealthCareEnums.HEALTHCARE_MZ.getCode().equals(emrtype)) {
                if (emrMap.RecordDetailNo_MZ(parem).size() > 0) {
                    map.put("vitalSigns", emrMap.RecordDetailNo_MZ(parem));
                } else {
                    map.put("vitalSigns", null);
                }
                if (emrMap.AdmissionNo_MZ(parem).size() > 0) {
                    //System.out.println(emrMap.AdmissionNo_MZ(parem));
                    map.put("medical", emrMap.AdmissionNo_MZ(parem).get(0));
                } else {
                    map.put("medical", null);
                }
            } else if (HealthCareEnums.HEALTHCARE_ZY.getCode().equals(emrtype)) {
                if (emrMap.RecordDetailNo_ZY(parem).size() > 0) {
                    map.put("vitalSigns", emrMap.RecordDetailNo_ZY(parem));
                } else {
                    map.put("vitalSigns", null);
                }
                if (emrMap.AdmissionNo_ZY(parem).size() > 0) {
                    //获取病程记录=============================开始
                    List<Map<String,Object>> bcjl_zy = emrMap.RecordInpNo_ZY(parem);
                    map.put("recordInp", bcjl_zy);
                    //获取病程记录=============================结束
                    Map<String,Object> basy_zy = emrMap.AdmissionNo_ZY(parem).get(0);
                    log.error("住院病案信息："+basy_zy);
                    map.put("medical", basy_zy);
                    //病案首页附页
                    basy_fy = new HashMap<>();
                    basy_fy.put("RYTJ",basy_zy.get("RYTJ"));
                    basy_fy.put("ZLLB",basy_zy.get("ZLLB"));
                    basy_fy.put("XYRYBQ",basy_zy.get("XYRYBQ"));
                    basy_fy.put("ZYRYBQ",basy_zy.get("ZYRYBQ"));
                    basy_fy.put("ZZYJH",basy_zy.get("ZZYJH"));
                    basy_fy.put("YBZFFS",basy_zy.get("YBZFFS"));
                    String path = HISUploadServiceImpl.class.getClassLoader().getResource("Movie.json").getPath();
                    path = URLDecoder.decode(path,"utf-8");
                    String s = JsonUtil.readJsonFile(path);
                    JSONObject jobj = JSON.parseObject(s);

                    JSONObject movies = jobj.getJSONObject("JGID"+jgid);//构建JSONArray数组
                    if(movies != null){
                        String jg = movies.get("jgno").toString();
                        if(EMR_FYEnums.EMRBASYFY_HZ.getCode().equals(jg)){//定义枚举
                            basy_fy = emrMap.AdmissionNo_ZYFY(parem).get(0);
                            JSONObject zllb = movies.getJSONObject("zllb");
                            if(zllb != null){
                                //治疗类别转换
                                basy_fy.put("ZLLB",zllb.get(basy_fy.get("ZLLB")));
                                basy_zy.put("ZLLB",zllb.get(basy_zy.get("ZLLB")));
                            }
                            JSONObject rjssbl = movies.getJSONObject("rjssbl");
                            if(rjssbl != null){
                                //治疗类别转换
                                basy_fy.put("RJSSBL",rjssbl.get(basy_fy.get("RJSSBL")));
                            }
                        }
                    }
                    map.put("MedicalAttach", basy_fy);
                    map.put("LeaveHospital", emrMap.Dischargesummary_ZY(parem).get(0)==null?new ArrayList<>():emrMap.Dischargesummary_ZY(parem).get(0));
                    List<Map<String,Object>> list_Operationrecords = emrMap.Operationrecords_ZY(parem);
                    for(int j=0;j<list_Operationrecords.size();j++){
                        list_Operationrecords.get(j).put("OperationRecordNo",list_Operationrecords.get(j).get("ClinicNo")+""+(j+1));
                    }
                    if("2".equals(hzflag)){
                        //非杭州市接口上传标准
                        List<Map<String,Object>> list_Operationrecord = null;
                        List<Map<String,Object>> list_Operationrecord_detail = null;
                        Map<String,Object> map_Operationrecord = null;
                        Map<String,Object> map_Operationrecord_detail = null;
                        list_Operationrecord = new ArrayList<>();
                        for(int k=0;k<list_Operationrecords.size();k++){
                            log.error("手术记录："+list_Operationrecords.get(k));
                            map_Operationrecord_detail = new HashMap<>();
                            list_Operationrecord_detail = new ArrayList<>();
                            map_Operationrecord = new HashMap<>();
//                            map_Operationrecord.put("OperationRecordNo", list_Operationrecords.get(k).get("OperationRecordNo"));
//                            map_Operationrecord.put("OperationDoctorCode", list_Operationrecords.get(k).get("OperationDoctorCode"));
//                            map_Operationrecord.put("OperationDoctorName", list_Operationrecords.get(k).get("OperationDoctorName"));
//                            map_Operationrecord.put("FirstOperDoctorCode", list_Operationrecords.get(k).get("FirstOperDoctorCode"));
//                            map_Operationrecord.put("FirstOperDoctorName", list_Operationrecords.get(k).get("FirstOperDoctorName"));
//                            map_Operationrecord.put("SecondOperDoctorCode", list_Operationrecords.get(k).get("SecondOperDoctorCode"));
//                            map_Operationrecord.put("SecondOperDoctorName", list_Operationrecords.get(k).get("SecondOperDoctorName"));
//                            map_Operationrecord.put("AnesthesiologistCode", list_Operationrecords.get(k).get("AnesthesiologistCode"));
//                            map_Operationrecord.put("AnesthesiologistName", list_Operationrecords.get(k).get("AnesthesiologistName"));
//                            map_Operationrecord.put("OperationDate", list_Operationrecords.get(k).get("OperationDate"));
//                            map_Operationrecord.put("OperationFinishDate", list_Operationrecords.get(k).get("OperationFinishDate"));
//                            map_Operationrecord.put("AnaesthesiaType", list_Operationrecords.get(k).get("AnaesthesiaType"));
//                            map_Operationrecord.put("IsComplication", list_Operationrecords.get(k).get("IsIatrogenic"));
//                            map_Operationrecord.put("ComplicationCode", list_Operationrecords.get(k).get("ComplicationCode"));
//                            map_Operationrecord.put("ComplicationName", list_Operationrecords.get(k).get("ComplicationName"));
//                            map_Operationrecord.put("OperationRecord", list_Operationrecords.get(k).get("OperationRecord"));
//                            map_Operationrecord.put("RecordDoctorCode", list_Operationrecords.get(k).get("RecordDoctorCode"));
//                            map_Operationrecord.put("RecordDoctorName", list_Operationrecords.get(k).get("RecordDoctorName"));
//                            if(k==0) {
//                                map_Operationrecord.put("OperationType", "1");
//                            }else{
//                                map_Operationrecord.put("OperationType", "2");
//                            }
                            /**
                             * 明细组装
                             */
                            if(!"".equals(list_Operationrecords.get(k).get("OperationCode"))&&null!=list_Operationrecords.get(k).get("OperationCode")){
                                log.error("手术明细："+list_Operationrecords.get(k));
                                map_Operationrecord.put("OperationRecordNo", list_Operationrecords.get(k).get("OperationRecordNo"));
                                map_Operationrecord.put("OperationDoctorCode", list_Operationrecords.get(k).get("OperationDoctorCode"));
                                map_Operationrecord.put("OperationDoctorName", list_Operationrecords.get(k).get("OperationDoctorName"));
                                map_Operationrecord.put("FirstOperDoctorCode", list_Operationrecords.get(k).get("FirstOperDoctorCode"));
                                map_Operationrecord.put("FirstOperDoctorName", list_Operationrecords.get(k).get("FirstOperDoctorName"));
                                map_Operationrecord.put("SecondOperDoctorCode", list_Operationrecords.get(k).get("SecondOperDoctorCode"));
                                map_Operationrecord.put("SecondOperDoctorName", list_Operationrecords.get(k).get("SecondOperDoctorName"));
                                map_Operationrecord.put("AnesthesiologistCode", list_Operationrecords.get(k).get("AnesthesiologistCode"));
                                map_Operationrecord.put("AnesthesiologistName", list_Operationrecords.get(k).get("AnesthesiologistName"));
                                map_Operationrecord.put("OperationDate", list_Operationrecords.get(k).get("OperationDate"));
                                map_Operationrecord.put("OperationFinishDate", list_Operationrecords.get(k).get("OperationFinishDate"));
                                map_Operationrecord.put("AnaesthesiaType", list_Operationrecords.get(k).get("AnaesthesiaType"));
                                map_Operationrecord.put("IsComplication", list_Operationrecords.get(k).get("IsIatrogenic"));
                                map_Operationrecord.put("ComplicationCode", list_Operationrecords.get(k).get("ComplicationCode"));
                                map_Operationrecord.put("ComplicationName", list_Operationrecords.get(k).get("ComplicationName"));
                                map_Operationrecord.put("OperationRecord", list_Operationrecords.get(k).get("OperationRecord"));
                                map_Operationrecord.put("RecordDoctorCode", list_Operationrecords.get(k).get("RecordDoctorCode"));
                                map_Operationrecord.put("RecordDoctorName", list_Operationrecords.get(k).get("RecordDoctorName"));
                                if(list_Operationrecords.get(k).get("OperationRecord")!=null){
                                    Clob clob = (Clob) list_Operationrecords.get(k).get("OperationRecord");
                                    String res = clob.getSubString((long) 1, (int) clob.length());
                                    JSONObject jsonObject = JSONObject.parseObject(res);
                                    map_Operationrecord.put("OperationRecord", jsonObject.getString("SSJGJCL"));
                                }else{
                                    map_Operationrecord.put("OperationRecord", "");
                                }

//                                if(list_Operationrecords.get(k).get("ZSSBZ").equals(""+(k+1))) {
//                                    map_Operationrecord.put("OperationType", "1");//是否急诊手术
//                                }else{
//                                    map_Operationrecord.put("OperationType", "2");
//                                }
                                map_Operationrecord.put("OperationType", "1");//是否急诊手术
                                map_Operationrecord_detail.put("OperationRecordNo",list_Operationrecords.get(k).get("OperationRecordNo"));
                                map_Operationrecord_detail.put("OperationNo",list_Operationrecords.get(k).get("OperationRecordNo"));
                                map_Operationrecord_detail.put("OperationCode",list_Operationrecords.get(k).get("OperationCode"));
                                map_Operationrecord_detail.put("OperationName",list_Operationrecords.get(k).get("OperationName"));
                                map_Operationrecord_detail.put("OperationLevel",list_Operationrecords.get(k).get("OperationLevel"));
                                map_Operationrecord_detail.put("OperationIncisionClass",list_Operationrecords.get(k).get("OperationIncisionClass"));
                                map_Operationrecord_detail.put("OperationHealClass",list_Operationrecords.get(k).get("OperationHealClass"));
//                                if(k==0) {
//                                    map_Operationrecord_detail.put("IsMajorIden", list_Operationrecords.get(k).get("ZSSBZ"));
//                                }else{
//                                    map_Operationrecord_detail.put("IsMajorIden", "0");
//                                }
                                if((""+(k+1)).equals(list_Operationrecords.get(k).get("ZSSBZ"))) {//
                                    map_Operationrecord_detail.put("IsMajorIden", "1");//主手术标志
                                }else{
                                    map_Operationrecord_detail.put("IsMajorIden", "0");
                                }
                                map_Operationrecord_detail.put("IsIatrogenic",list_Operationrecords.get(k).get("IsIatrogenic"));
                                list_Operationrecord_detail.add(map_Operationrecord_detail);
                                map_Operationrecord.put("ListOperationDetail",list_Operationrecord_detail);
                                list_Operationrecord.add(map_Operationrecord);
                            }else{
                                //map_Operationrecord.put("OperationRecordNo", "-");
                            }
                        }
                        log.error("手术记录："+list_Operationrecord);
                        //传输节点改变
                        list_Operationrecords = list_Operationrecord;
                    }
                    //判断手术是否为空
                        if("2".equals(hzflag)){
                            if(list_Operationrecords.size()!=0){
                                log.error("xxxxx："+list_Operationrecords.get(0).get("OperationDoctorCode"));
                                if(list_Operationrecords.get(0).get("OperationDoctorCode")==null){
                                    //list_Operationrecords = new ArrayList<>();
                                    //log.error("");
                                }
                            }
                        }
                    map.put("Operation", list_Operationrecords);
                } else {
                    map.put("medical", null);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResponseBean.fail(HISUploadDataEnums.FAIL_DB_HEALTHCARE.getCode(),HISUploadDataEnums.FAIL_DB_HEALTHCARE.getDescription(),e.getMessage());
        }
        return ResponseBean.success("查询上传医保数据成功",map);
    }

    @Override
    public ResponseBean healthCare_new(String jgid, String mcno, String docid) {
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> param = new HashMap<>();
        param.put("jgid",jgid);
        param.put("mcno",mcno);
        param.put("docid",docid);

        //查询病人是否归档
        int count = emrMap.getEmrArchiveStatus(param);
        if(count==0){
            log.error("参数{}，查询结果{}",param,HISUploadDataEnums.FAIL_XT_ARCHIVE.getDescription());
            return ResponseBean.fail(HISUploadDataEnums.FAIL_XT_ARCHIVE.getCode(),HISUploadDataEnums.FAIL_XT_ARCHIVE.getDescription(),"病人数据未归档，请核对表数据后重试！！！");
        }
        try {
            map.put("Admission", emrMap.getAdmissionInformation(param));//入院信息
            map.put("BaseInfo", emrMap.getBaseInformation(param));//基本信息
            map.put("Diagnosis", emrMap.getDiagnosisList(param));//诊断信息
            map.put("Surgery", emrMap.getSurgeryList(param));//手术信息
            map.put("EMR_Diagnosis", emrMap.getEMR_DiagnosisList(param));//电子病历诊断信息
            map.put("EMR_Progress", emrMap.getEMR_ProgressList(param));//电子病历病程记录
            map.put("EMR_Surgery", emrMap.getEMR_SurgeryList(param));//电子病历手术信息
            map.put("EMR_Discharge", emrMap.getEMR_DischargeList(param));//电子病历出院小结
            map.put("EMR_Die", emrMap.getEMR_DeathList(param));//电子病历死亡记录
            log.info("参数{}，查询结果{}",param,map);
        }catch(Exception e){
            e.printStackTrace();
            log.error("错误日志{}",e.toString());
            log.error("参数{}，查询结果{}",param,map);
            return ResponseBean.fail(HISUploadDataEnums.FAIL_DB_EMRPAT.getCode(),e.getMessage(),e.toString());
        }
        return ResponseBean.success("查询上传医保数据成功",map);
    }

    //获取病案首页附页信息
    public Map<String,Object> getBASYFY(String jgid){
        if(jgid==null){

        }
        return null;
    }


    @Override
    public ResponseBean upHisInfo(String data) {
        /**
         * 转化实体
         */
        JSONObject jo = JSONObject.parseObject(data);
        PATIENT_INFORMATION_MZ bean = JSON.parseObject(jo.get("data").toString(),PATIENT_INFORMATION_MZ.class);
        List<PATIENT_INFORMATION_MZ> list = emrMap.queryMzPatientInfo(bean);
        try {
            if (list != null && list.size() > 0) {
                // 数据已经存在 更新记录
                bean.setEmpId(list.get(0).getEmpId());
                emrMap.updateMzPatientInfo(bean);

            } else {
                bean.setEmpId(UUID.randomUUID() + "");
                emrMap.insertMzPatientInfo(bean);
            }
            List<PATIENT_INFORMATION_MZ> list_ = emrMap.queryMzPatVisiting(bean);
            if (list_ != null && list_.size() > 0) {
                // 数据已经存在 更新记录
                emrMap.updateMzPatVisiting(bean);
            } else {
                emrMap.insertMzPatVisiting(bean);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResponseBean.fail(HISUploadDataEnums.FAIL_DB_EMRPAT_MZ.getCode(),HISUploadDataEnums.FAIL_DB_EMRPAT_MZ.getDescription(),e.getMessage());
        }
        return ResponseBean.success("同步门诊病人基本信息成功",bean);
    }

    @Override
    public ResponseBean upDicBRXZ(String data) {
        JSONObject jo = JSONObject.parseObject(data);
        List<Map> list=JSON.parseArray(jo.get("list").toString(),Map.class);
        if(list.size()>0){
            emrMap.deleteBRXZ(list.get(0));
            emrMap.saveBRXZ(list);
        }
        return ResponseBean.success("同步门诊病人基本信息成功",list);
    }

    @Override
    public ResponseBean queryRecLisInpatient(String data) {
            JSONObject jo = JSONObject.parseObject(data);
            jo = (JSONObject)jo.get("data");
            Map<String, Object> map = new HashMap<String, Object>();
            //机构id
            String jgid = jo.get("jgid").toString();
            String mcNo = jo.get("mcNo").toString();
            String docId = jo.get("docId").toString();
            String mzzy = jo.get("mzzy").toString();
            String mzhm = jo.get("mzhm").toString();
            map.put("jgid", jgid);
            map.put("mcNo", docId);
            map.put("docId", docId);
            map.put("mzzy", mzzy);
            map.put("mzhm", mzhm);
            List<Map<String, Object>> brxxList = null;
            if (mzzy.equals("1")) {
                map.put("mzhm", mzhm);
                brxxList = emrMap.queryRecLisInpatient(map);
            }
        return ResponseBean.success("同步门诊病人基本信息成功",brxxList);
    }

    @Override
    public ResponseBean upreclis(String data) {
        /**
         * 转化实体
         */
        JSONObject jo = JSONObject.parseObject(data);
        LIS_YBXX bean = JSON.parseObject(jo.get("data").toString(),LIS_YBXX.class);
        LIS_YBXX bean_ = emrMap.querylisybxx(bean);
        try {
            if (bean_ == null) {
                emrMap.insertlisybxx(bean);
            } else {
                emrMap.updatelisybxx(bean);
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResponseBean.fail(HISUploadDataEnums.FAIL_DB_LISYBXX.getCode(),HISUploadDataEnums.FAIL_DB_LISYBXX.getDescription(),e.getMessage());
        }
        return ResponseBean.success("同步LIS样本信息成功");
    }

    @Override
    public ResponseBean upreclisdetail(String data) {
        JSONObject jo = JSONObject.parseObject(data);
        JSONArray arr = JSONObject.parseArray(jo.get("data").toString());
        ResponseBean result = ResponseBean.success("同步LIS样本明细信息成功");
        List<LIS_YBXX_R> list = new ArrayList<>();
        for(int i = 0; i < arr.size(); i++) {
            JSONObject obj = arr.getJSONObject(i);
            LIS_YBXX_R bean = JSON.parseObject(obj.toString(), LIS_YBXX_R.class);
            LIS_YBXX_R temp = emrMap.querylisybxxr(bean);
            try{
                if(temp!=null){
                    emrMap.updatelisybxxr(bean);
                }else{
                    emrMap.insertlisybxxr(bean);
                }
            }catch(Exception e){
                //保存日志，返回数据,将不成功的信息返回
                list.add(bean);
            }
        }
        if(list.size()>0){
            result.setCode(HISUploadDataEnums.FAIL_DB_LISYBXX_DETAIL.getCode());
            result.setData(list);
            result.setMsg(HISUploadDataEnums.FAIL_DB_LISYBXX_DETAIL.getDescription());
        }
        return result;
    }

    public static void main(String[] args) {
        Object[] a = {"3",2,"1"};
        String[] b = Arrays.stream(a).map(x -> x.toString()).toArray(String[]::new);
        System.out.println(b);
        for(Object c:b){
            System.out.println(c);
        }
    }
}
